import { FriendItem } from './IFriend';
import { GroupItem, VirualGroupItem, VirualGroupMember } from './IGroup';
import { ChatType, MessageBodyExtend } from '../net/Const';
import { observable } from 'mobx';
import { EmployItem } from './IDept';
import { UserType } from '../const';

export class MessageItem {
    // messageId:string;
    // fromUserId:string;
    // fromUserName: string;
    // content:string;
    // timeSend: string;
    // type: number;
    // toUserId: string;
    // toUserName: string;

    chatType: string = 'chat';
    to: string;
    from: string = '';
    id: string;
    toJid: string;
    /** 真实客服用户ID  */
    fromUserServiceId?:string;
    /**
     * 公众号ID
     */
    fromUserPublicId?:string
    /**
     * 真实客服ID
     */
    //fromUserServiceId?:string;
    // toTransferUser?:{
    //     /** 真实客服ID  */
    //     userId:number,
    //     nickname?:string,
    //     /**
    //      * 转接消息备注
    //      */
    //     transferRemark?:string,
    // }
    // 转接消息备注
    remark?:string;
    messageId: string;
    fromUserId: string;
    fromUserName: string;
    content: string
    /** 同contentExtend.reRouterUserId */
    reRouterUserId?: string;
    reRouterUserName?: string;

    contentExtend?: MessageBodyExtend;
    timeSend: string;
    type: number;
    // 将代替过去的type
    userType?:number;
    isEncrypt?: number;
    isReadDel?: number;
    toUserId: string;
    toUserName: string;
    fileSize?: string;
    jid: string;
    objectId?: string;
    location_x?: string
    location_y?: string
    timeLen?: number;
    roomJid?: string;
    fileName?: string;
    contentType?: number;
    isRead?: number;
    verification?: number;//群验证结果：0:待确认  1：已确认  2：已拒绝
    readDeadTime?: number
    myReadDeadTime?: number
    direction?: boolean = false
    text?:string
    onlinestate?:number
   @observable unreadCount:number=0
    static getMessageItem(_messageItem: MessageItem | any): MessageItem {

        let messageItem: MessageItem = new MessageItem();
        for (let key in _messageItem) {
            messageItem[key] = _messageItem[key];
        }
        return messageItem;
    }
    static getMessageBylastmes(_messageItem: any): MessageItem {
        return {
            chatType: _messageItem.isRoom ? ChatType.GROUPCHAT : ChatType.CHAT,
            to: (_messageItem.isRoom || !_messageItem.to) ?_messageItem.jid:_messageItem.to,
            from: _messageItem.from,
            id: _messageItem._id,
            toJid: _messageItem.jid || (_messageItem.toJid ? _messageItem.toJid : ''),
            messageId: _messageItem.messageId,
            fromUserId: _messageItem.from,
            fromUserName: _messageItem.fromUserName,
            content: _messageItem.content,
            timeSend: _messageItem.timeSend,
            type: _messageItem.type,
            userType:_messageItem.userType || 0,
            toUserId: _messageItem.to,
            toUserName: _messageItem.toUserName,
            jid: _messageItem.isRoom ? _messageItem.jid : '',
            unreadCount:_messageItem._messageItem,
            onlinestate:_messageItem.onlinestate || 0,
        }
    }

}
export type MessageList = MessageItem[];
export enum MessageStatusType {
    red,
    loading,
    sent,
    error
}
export const MessageStatus = {
    [MessageStatusType.red]: 'red',
    [MessageStatusType.loading]: 'loading',
    [MessageStatusType.sent]: 'send',
}
export type MessageStatus = Map<string, MessageStatusType>
export type MessageData = Map<string, MessageList>

export class ChatItem {
    /**
     * 最后一次聊天内容
     */
    lastContent: string;
    content?:string;
    /** 可能是用户ID & 公众号ID，即聊天会话ID。 */
    id: string;
    /**
     * currentChatData时的动态ID,可能是用户ID & 公众号ID，即聊天会话ID。
     * 当分流来回切换时，这个数据会变化
    */
    dynamicId?:string;
    /**
     * 客服真实ID
     */
    dynamicServiceUserId?:string;

    /** 多标签ID,格式为：${tag}_id */
    idWithTag?:string;
    /** 最后一次聊天时间 */
    lastTime: string;
    messageId: string;
    name: string;
    timeSend: number;
    type: number;
    gid: string = "";
    jid?:string
    toUserName?:string
    isForbidden:boolean;//是否禁言

    // 是否被客服接待中
    
    isServicingUserId?:string;


    /** 个人的话就显示最近登录时间、群的话就显示群成员数量和在线人数*/
    desc?: string = '';
    nickname?: string = '';
    remarkName?: string = '';
    role?: number;
    msgTalkTime?: number
    allowFriends?: boolean;
    groupInvitNeedTest?: boolean;
    isTop?: boolean;
    isListTop?: boolean; // 是否列表置顶，优先级低于isTop
    isMute?: boolean;
    isMuteAll?: boolean;
    isMuteSpeaker?: boolean;
    fileName?: string;
    isNotice?: boolean;
    mentionText?: string;
    snapChat?: number;
    isBacklock?:boolean;//群后台是否锁定
    /** 在线状态 */
    onlinestate?:number
    /** 来一个大而全的用户详情 */
    extendInfo?:any
    /** 标签组 */
    tagGroups:VirualGroupItem[]=[]
    /** 用户所在用户组 */
    userGroups:VirualGroupItem[]=[]
    userType?:UserType
    toUserType?:UserType

    static getChatItem(_bindHost: FriendItem | GroupItem | MessageItem | EmployItem | VirualGroupMember, revert:boolean = false): ChatItem {
        let chatItem: ChatItem = new ChatItem();
        const vo:any = _bindHost
        // 如果开启了Revert，vo没定义direction，则进行翻转
        let direction = false;
        if( revert ){
            if( typeof vo['direction']=='undefined' ){
                if( vo.toJid && vo.from && vo.to ){
                    if( vo.toJid == vo.from ){
                        direction = true;
                    }
                }
            }else{
                direction = false;
            }
        }
        
        if( direction ){
            // 处理to和from的数据翻转
            (chatItem as any).to = vo.from;
            (chatItem as any).toUserId = vo.from;
            (chatItem as any).toUserName = vo.fromUserName;
            (chatItem as any).from = vo.to;
            (chatItem as any).fromUserId = vo.to;
            (chatItem as any).fromUserName = vo.toUserName;
            chatItem.id = vo.from;
            //反转
            vo['toUserId'] = vo.from;
            vo['toUserName'] = vo.fromUserName;
        }
        //某些函数将jid转为了toJid
        vo.jid = vo.jid || vo.toJid || '';
        if (vo['toUserId']) {
            // 默认ID是toUserId
            chatItem.id = vo['toUserId'];
            chatItem.type = ChatGrOrFrType.friend;
        } else if (vo['jid']) {
            chatItem.id = vo['jid'];
            chatItem.gid = vo['id'];
            chatItem.type = ChatGrOrFrType.group;
        }else if( vo['userId']){
            // EmplyItem
            chatItem.id = vo['userId'];
            chatItem.type = ChatGrOrFrType.employ;
        }
        
        if( !chatItem.name ){
            chatItem.name = vo.toNickname || vo.nickname || vo.name || vo.toUserName
        }
        
        // 群聊，名称显示群名称
        if( chatItem.type == 902 && vo.content ){
            chatItem.name = vo.content;
        }
        chatItem.onlinestate = vo.onlinestate || 0
        chatItem.lastTime = vo.lastTime || '';
        chatItem.lastContent = vo.lastContent || '';
        chatItem.messageId = vo.messageId || '';
        if( vo.userType ){
            chatItem.userType = Number(vo.userType)
        }
        if( isNaN( Number(chatItem.userType) ) ){
            // 在好友列表里，要访问toUserType
            chatItem.userType = Number(vo.toUserType)
        }
        chatItem.content = vo.content || '';
        chatItem.lastContent = vo.lastContent || vo.content;
        chatItem.mentionText = vo.mentionText || '';
        

        chatItem.remarkName = (_bindHost as any).remarkName || '';
        if( vo.tagNames ){
            // 标签组
            chatItem.tagGroups = vo.tagNames.map((item:any)=>{
                return new VirualGroupItem({
                    name:item.name,
                }) 
            })
        }
        //chatItem.userType = Number(vo.userType)
        return chatItem;
    }
}
export type ChatList = ChatItem[];

export class ChatData extends ChatItem{
    /** chatData刷新键 */
    public refreshKey?:string;
    constructor(){
        super()
    }
}
// export class EmojiItem {
//     url: string;
//     name: string

//     static getEmojiItem(_emoji: any): EmojiItem {
//         let emoji = new EmojiItem();
//         for (let key in _emoji) {
//             emoji[key] = _emoji[key];
//         }
//         return emoji;
//     }
// }
// export type EmojiList = EmojiItem[];

export interface ChatNoReaderMessageData {
    [chatId: string]: MessageItem[]
}
export interface ChatOnlineTime {
    [friendId: string]: string
}
export enum DrawerType {
    friend,
    group,
    notice,
}
export interface GroupMemeberItem {
    name: string,
    headUrl: string,
    groupUserId: string,
    role: string,
}
export interface GroupMember {
    [groupId: string]: GroupMemeberItem[]
}


export enum ChatGrOrFrType {
    friend,
    group,
    employ
}
/**
 * 1=普通用户；  2=公众号 ；3=机器账号，由系统自动生成；4=客服账号, 5=企业用户客服
 */
export enum ChatUserType {
    user = 1,
    public,
    system,
    service,
    /**
     * 企业用户客服
     */
    employService,
}
export interface NoticeItem {
    id: string,
    nickname: string,
    roomId: string,
    text: string,
    time: string,
    userId: string,
}
// export interface NoticeItem {
//     id: string,
//     nickname: string,
//     roomId: string,
//     text: string,
//     time: string,
//     userId: string,
// }
export type GroupControlState = Map<string, boolean | number>;

export interface IServerUrlItem {
    name: string,
    url: string
}
